package org.happen.android.widget;

import java.util.Timer;
import java.util.TimerTask;

import org.liushui.mycommons.android.util.McDimenUtil;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import org.happen.android.R;

public class WidgetViewPager extends RelativeLayout
{
	ViewPager viewPager;
	LinearLayout viewGroupDot;
	Context context;
	OnPageChangeListener onPageChangeListener;
	MyHandler mHandler = new MyHandler();
	int adapterCount;
	int lastType;
	Timer timer;
	boolean isRun;

	public WidgetViewPager(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
	}

	protected void onFinishInflate()
	{
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		viewGroupDot = (LinearLayout) findViewById(R.id.viewGroupDot);

		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			public void onPageSelected(int arg0)
			{
				for (int i = 0; i < viewGroupDot.getChildCount(); i++)
				{
					int bg = i == arg0 % adapterCount ? R.drawable.dian_sel : R.drawable.dian;
					viewGroupDot.getChildAt(i).setBackgroundResource(bg);
				}
				if (onPageChangeListener != null)
				{
					onPageChangeListener.onPageSelected(arg0);
				}
			}

			public void onPageScrolled(int arg0, float arg1, int arg2)
			{
				if (onPageChangeListener != null)
				{
					onPageChangeListener.onPageScrolled(arg0, arg1, arg2);
				}
			}

			public void onPageScrollStateChanged(int arg0)
			{
				if (onPageChangeListener != null)
				{
					onPageChangeListener.onPageScrollStateChanged(arg0);
				}
			}
		});
	}

	public void setAdapter(PagerAdapter pagerAdapter, int count)
	{
		viewPager.setAdapter(pagerAdapter);
		adapterCount = count;
		viewGroupDot.removeAllViews();
		int m = McDimenUtil.dp2Px(5);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(m, 0, m, 0);
		for (int i = 0; i < count; i++)
		{
			ImageView imageView = new ImageView(context);
			if (i == 0)
			{
				imageView.setBackgroundResource(R.drawable.dian_sel);
			} else
			{
				imageView.setBackgroundResource(R.drawable.dian);
			}

			final int currentItem = i;
			imageView.setOnClickListener(new OnClickListener() {
				public void onClick(View v)
				{
					viewPager.setCurrentItem(currentItem, true);
				}
			});
			viewGroupDot.addView(imageView, lp);
		}
		invalidate();
	}

	public OnPageChangeListener getOnPageChangeListener()
	{
		return onPageChangeListener;
	}

	public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener)
	{
		this.onPageChangeListener = onPageChangeListener;
	}

	public ViewPager getViewPager()
	{
		return viewPager;
	}

	public LinearLayout getViewGroupDot()
	{
		return viewGroupDot;
	}

	public void runfinsh()
	{
		timer.cancel();
	}

	public void pause()
	{
		isRun = false;
	}

	public void start()
	{
		isRun = true;
	}

	public void run()
	{
		if (timer == null)
		{
			timer = new Timer(true);
			isRun = true;
		}
		timer.schedule(task, 1000, 4000);
	}

	TimerTask task = new TimerTask() {
		public void run()
		{
			if (isRun)
			{
				Message m = mHandler.obtainMessage();
				m.arg1 = viewPager.getCurrentItem() + 1;
				m.sendToTarget();
			}
		}
	};

	class MyHandler extends Handler
	{
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			viewPager.setCurrentItem(msg.arg1);
		}
	}
}